//
//  MIneDoLikeCell.m
//  ShowYork
//
//  Created by hxh on 2020/11/16.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineDoLikeCell.h"

@interface MineDoLikeCell ()
@property (nonatomic,strong)UIImageView *headerIcon;
@property (nonatomic,strong)UIImageView *thumbnailIcon;
@property (nonatomic,strong)UIImageView *videoIcon;
@property (nonatomic,strong)UILabel *nameLb;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UILabel *timeLb;
@property (nonatomic,strong)UIView *bageView;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIView *postContentView;
@property (nonatomic,strong)UILabel *postContentLb;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,strong)UIButton *thumbnailBtn;


@end

@implementation MineDoLikeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
        
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    
    self.headerIcon = [[UIImageView alloc]init];
    self.headerIcon.layer.cornerRadius = 20;
    self.headerIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerIcon];
    
    [self.headerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(16);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    self.bageView = [[UIView alloc]init];
    self.bageView.backgroundColor = RGBA(232, 85, 83, 1);
    self.bageView.layer.cornerRadius = 3;
    self.bageView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bageView];
    [self.bageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(5);
        make.size.mas_equalTo(CGSizeMake(6, 6));
    }];
    self.nameLb = [[UILabel alloc]init];
    self.nameLb.textColor = C51;
    self.nameLb.font = font(14);
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.headerIcon.mas_right).offset(16);
    }];
    
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.textColor = C102;
    self.contentLb.font = font(14);
    [self.contentView addSubview:self.contentLb];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(2);
        make.left.equalTo(self.nameLb);
    }];

    self.timeLb = [[UILabel alloc]init];
    self.timeLb.textColor = C153;
    self.timeLb.font = font(10);
    [self.contentView addSubview:self.timeLb];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLb.mas_bottom).offset(4);
        make.left.equalTo(self.nameLb);
        make.bottom.equalTo(self.contentView).offset(-16);
    }];
    self.thumbnailIcon = [[UIImageView alloc]init];
    self.thumbnailIcon.backgroundColor = RGBA(240, 240, 240, 1);
    self.thumbnailIcon.contentMode = UIViewContentModeScaleAspectFill;
    self.thumbnailIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:self.thumbnailIcon];
    [self.thumbnailIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-16);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    self.postContentView = [[UIView alloc]init];
    self.postContentView.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.postContentView];
    
    self.postContentLb = [[UILabel alloc]init];
    self.postContentLb.textColor = C102;
    self.postContentLb.numberOfLines = 4;
    self.postContentLb.font = font(12);
    [self.postContentView addSubview:self.postContentLb];
    
 
    [self.postContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.thumbnailIcon);
    }];
    
    [self.postContentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.postContentView);
    }];
    
    self.videoIcon = [[UIImageView alloc]init];
    self.videoIcon.image = [UIImage imageNamed:@"mine_video_tag"];
    [self.contentView addSubview:self.videoIcon];
    [self.videoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.thumbnailIcon);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.mas_equalTo(self.contentView);
        make.left.equalTo(self.nameLb);
        make.height.mas_equalTo(0.5);
    }];
    
    self.thumbnailBtn = [[UIButton alloc]init];
    [self.thumbnailBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.thumbnailBtn];
    [self.thumbnailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.thumbnailIcon);
    }];
    
    self.headerIcon.userInteractionEnabled = YES;
    UITapGestureRecognizer *headerIconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.headerIcon addGestureRecognizer:headerIconTap];
    self.nameLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *nameLbTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showUserInfo)];
    [self.nameLb addGestureRecognizer:nameLbTap];
    
}

- (void)showUserInfo{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showUserInfoById:)]) {
        [self.delegate showUserInfoById:self.itemModel.sendUserId];
    }
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showDetailByItemModel:)]) {
        [self.delegate showDetailByItemModel:self.itemModel];
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"MineDoLikeCell";
    MineDoLikeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[MineDoLikeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setItemModel:(MineMessageItemModel *)itemModel{
    _itemModel = itemModel;
        
    self.timeLb.text = itemModel.createStr;
    self.nameLb.text = itemModel.sendUserName;
    [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:itemModel.imagePath] placeholderImage:[UIImage imageNamed:@"defaut_header"]];
    if (itemModel.unread.intValue == 1) {
        self.bageView.hidden = YES;
    }else{
        self.bageView.hidden = NO;
    }
    self.videoIcon.hidden = YES;
    self.postContentLb.text = itemModel.postText;
    self.postContentView.hidden = YES;
    if(itemModel.postType.intValue == 1){
        self.thumbnailIcon.hidden = YES;
        self.postContentView.hidden = NO;
        self.postContentLb.text = itemModel.postText;
    }
    if (itemModel.postType.intValue == 2) {
        self.postContentLb.text = @"";
        self.thumbnailIcon.hidden = NO;
        [self.thumbnailIcon sd_setImageWithURL:[NSURL URLWithString:itemModel.postUrl]];
    }
    if (itemModel.postType.intValue == 3) {
        self.postContentLb.text = @"";
        self.videoIcon.hidden = NO;
        self.thumbnailIcon.hidden = NO;
        [self.thumbnailIcon sd_setImageWithURL:[NSURL URLWithString:itemModel.postUrl]];
    }
    // 评论
    if (itemModel.detailType.intValue == 1) {
        if (itemModel.contentType.intValue == 1) {
            self.contentLb.text = @"赞了你的动态";
        }else if(itemModel.contentType.intValue == 2){
            self.contentLb.text = @"赞了你的作品";
        }
    }else{
        if (itemModel.contentType.intValue == 5) {
            self.contentLb.text = @"赞了你的回答";
        }else{
            self.contentLb.text = @"赞了你的评论";
        }
    }
    self.timeLb.text = itemModel.createStr;
    
}
@end
